package com.cellulam.framework.scaffold.generate.utils;

import com.cellulam.framework.scaffold.generate.db.JdbcConfig;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Properties;

public class GenUtils {

    public static String dotToSlash(String _package) {
        return _package.replace('.', '/');
    }

    public static void recreateFile(String toPath, String content) throws IOException {
        deleteFile(new File(toPath));
        tryCreateParent(toPath);
        IOUtils.write(content.getBytes(), new FileOutputStream(new File(toPath)));
        System.out.println("\t\t create " + toPath);
    }

    public static void recreateFile(String toPath, String content, boolean ifExistThenCancel) throws IOException {
        File toFile = new File(toPath);
        if (toFile.exists() && ifExistThenCancel) {
            return;
        }
        deleteFile(new File(toPath));
        tryCreateParent(toPath);

        IOUtils.write(content.getBytes(), new FileOutputStream(new File(toPath)));

        System.out.println("\t\t create " + toPath);
    }

    public static void tryCreateParent(String toPath) {
        String parentPath = new File(toPath).getParent();
        if (!new File(parentPath).exists()) {
            new File(parentPath).mkdirs();
        }
    }


    public static void deleteFile(File f) throws IOException {
        if (f.exists()) {
            System.out.println("\t\t delete " + f.getName() + ", path is " + f.getParentFile().getAbsolutePath());
            new File(f.getAbsolutePath()).delete();
        }
    }

    public static String getResourceContent(String resourceName) {
        InputStream is = GenUtils.class.getClassLoader().getResourceAsStream(resourceName);
        try {
            String cont = new String(IOUtils.toByteArray(is));
            is.close();
            return cont;
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("getResourceContent error.", e);
        }
    }

    public static String getResourcePath(String resource) {
        URL url = GenUtils.class.getClassLoader().getResource(resource);
        return url == null ? "" : url.getPath();
    }

    public static File tryGetResourceFile(String resource) {
        if (StringUtils.isEmpty(resource)) { return null; }

        String resourcePath = getResourcePath(resource);
        File resourceFile = StringUtils.isNotEmpty(resourcePath) ? new File(resourcePath) : new File(resource);

        if (!resourceFile.exists()) {
            resourceFile = null;
        }

        return resourceFile;
    }

    public static void info(String info, String prefix) {
        System.out.println(prefix + info);
    }

    public static boolean fileExists(String filePath) {
        return new File(filePath).exists();
    }

    public static String trimDbName(String dbName) {
        StringBuilder sb = new StringBuilder();
        for (char c : dbName.toCharArray()) {
            if (Character.isDigit(c) || Character.isAlphabetic(c)) {
                sb.append(c);
            }
        }
        return sb.toString();
    }


    public static Properties loadJdbcProp(Class<?> clazz, String resourceName) {
        try {
            InputStream is = clazz.getClassLoader().getResourceAsStream(resourceName);
            Properties props = new Properties();
            props.load(is);
            is.close();
            return props;
        } catch (IOException e) {
            throw new RuntimeException("loadJdbcProp error.", e);
        }
    }

    public static JdbcConfig createJdbcProp(Properties loadedProp, String dbName, String dbPackageName) throws IOException {
        String jdbcDriver = loadedProp.getProperty("jdbc.driver");
        String jdbcUrl = loadedProp.getProperty("jdbc.url").replace("{database}", dbName);
        String username = loadedProp.getProperty("jdbc.username");
        String password = loadedProp.getProperty("jdbc.password");



        return new JdbcConfig(jdbcDriver, jdbcUrl, dbName, dbPackageName, username, password);
    }

    public static String getTopDir(Class<?> clazz, String currentArtifactId, String resourcePath) {
        String propPath = clazz.getClassLoader().getResource(resourcePath).getPath();
        System.out.println("propPath is " + propPath);

        String topDir = propPath.substring(0, propPath.indexOf("/" + currentArtifactId));
        System.out.println("topDir is " + topDir);

        return topDir;
    }

    public static void main(String[] args) {
        System.out.println("" + trimDbName("ddd_aaaa112_bb*bbb"));
    }

}
